package com.ruoyi.web.controller.bussiness;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.AttendanceBo;
import com.ruoyi.system.domain.vo.AttendanceVo;
import com.ruoyi.system.service.IAttendanceService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import java.util.Arrays;
import java.util.List;

/**
 * 考勤到岗信息
 *
 * @author ruoyi
 * @date 2025-06-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/attendance")
public class AttendanceController extends BaseController {

    private final IAttendanceService iAttendanceService;

    /**
     * 查询考勤到岗信息列表
     */
    @SaCheckPermission("system:attendance:list")
    @GetMapping("/list")
    public TableDataInfo<AttendanceVo> list(AttendanceBo bo, PageQuery pageQuery) {
        return iAttendanceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出考勤到岗信息列表
     */
    @SaCheckPermission("system:attendance:export")
    @Log(title = "考勤到岗信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AttendanceBo bo, HttpServletResponse response) {
        List<AttendanceVo> list = iAttendanceService.queryList(bo);
        ExcelUtil.exportExcel(list, "考勤到岗信息", AttendanceVo.class, response);
    }

    /**
     * 获取考勤到岗信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:attendance:query")
    @GetMapping("/{id}")
    public R<AttendanceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iAttendanceService.queryById(id));
    }

    /**
     * 新增考勤到岗信息
     */
    @SaCheckPermission("system:attendance:add")
    @Log(title = "考勤到岗信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AttendanceBo bo) {
        return toAjax(iAttendanceService.insertByBo(bo));
    }

    /**
     * 修改考勤到岗信息
     */
    @SaCheckPermission("system:attendance:edit")
    @Log(title = "考勤到岗信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AttendanceBo bo) {
        return toAjax(iAttendanceService.updateByBo(bo));
    }

    /**
     * 删除考勤到岗信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:attendance:remove")
    @Log(title = "考勤到岗信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAttendanceService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
